{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# caffe relay 网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "import set_env"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from tvm.contrib.download import download_testdata\n",
    "from caffe import params as P\n",
    "from caffe_utils import _test_network"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## caffe Mobilenetv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _test_mobilenetv2(data):\n",
    "    \"\"\"One iteration of Mobilenetv2\"\"\"\n",
    "    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)\n",
    "    mean_val = np.reshape(mean_val, (1, 3, 1, 1))\n",
    "    mean_val = np.tile(mean_val, (1, 1, 224, 224))\n",
    "    data_process = data - mean_val\n",
    "    data_process = data_process / 58.8\n",
    "    data_process = data_process.astype(np.float32)\n",
    "\n",
    "    proto_file_url = (\n",
    "        \"https://github.com/shicai/MobileNet-Caffe/raw/master/mobilenet_v2_deploy.prototxt\"\n",
    "    )\n",
    "    blob_file_url = (\n",
    "        \"https://github.com/shicai/MobileNet-Caffe/blob/master/mobilenet_v2.caffemodel?raw=true\"\n",
    "    )\n",
    "    proto_file = download_testdata(proto_file_url, \"mobilenetv2.prototxt\", module=\"model\")\n",
    "    blob_file = download_testdata(blob_file_url, \"mobilenetv2.caffemodel\", module=\"model\")\n",
    "    _test_network(data_process, proto_file, blob_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)\n",
    "_test_mobilenetv2(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## caffe Alexnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pytest\n",
    "def _test_alexnet(data):\n",
    "    \"\"\"One iteration of Alexnet\"\"\"\n",
    "    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)\n",
    "    mean_val = np.reshape(mean_val, (1, 3, 1, 1))\n",
    "    mean_val = np.tile(mean_val, (1, 1, 227, 227))\n",
    "    data_process = data - mean_val\n",
    "    data_process = data_process.astype(np.float32)\n",
    "\n",
    "    proto_file_url = (\n",
    "        \"https://github.com/BVLC/caffe/raw/master/models/\" + \"bvlc_alexnet/deploy.prototxt\"\n",
    "    )\n",
    "    blob_file_url = \"http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel\"\n",
    "    proto_file = download_testdata(proto_file_url, \"alexnet.prototxt\", module=\"model\")\n",
    "    blob_file = download_testdata(blob_file_url, \"alexnet.caffemodel\", module=\"model\")\n",
    "    _test_network(data_process, proto_file, blob_file)\n",
    "\n",
    "\n",
    "@pytest.mark.skip(reason=\"See https://github.com/apache/tvm/issues/13227\")\n",
    "def test_forward_Alexnet():\n",
    "    \"\"\"Alexnet\"\"\"\n",
    "    data = np.random.randint(0, 256, size=(1, 3, 227, 227)).astype(np.float32)\n",
    "    _test_alexnet(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## caffe Resnet50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _test_resnet50(data):\n",
    "    \"\"\"One iteration of Resnet50\"\"\"\n",
    "    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)\n",
    "    mean_val = np.reshape(mean_val, (1, 3, 1, 1))\n",
    "    mean_val = np.tile(mean_val, (1, 1, 224, 224))\n",
    "    data_process = data - mean_val\n",
    "    data_process = data_process.astype(np.float32)\n",
    "\n",
    "    proto_file_url = (\n",
    "        \"https://github.com/fernchen/CaffeModels/raw/master/resnet/ResNet-50-deploy.prototxt\"\n",
    "    )\n",
    "    blob_file_url = (\n",
    "        \"https://github.com/fernchen/CaffeModels/raw/master/resnet/ResNet-50-model.caffemodel\"\n",
    "    )\n",
    "\n",
    "    proto_file = download_testdata(proto_file_url, \"resnet50.prototxt\", module=\"model\")\n",
    "    blob_file = download_testdata(blob_file_url, \"resnet50.caffemodel\", module=\"model\")\n",
    "\n",
    "    _test_network(data_process, proto_file, blob_file)\n",
    "\n",
    "\n",
    "def test_forward_Resnet50():\n",
    "    \"\"\"Resnet50\"\"\"\n",
    "    data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)\n",
    "    _test_resnet50(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## caffe Inceptionv4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _test_inceptionv1(data):\n",
    "    \"\"\"One iteration of Inceptionv4\"\"\"\n",
    "    mean_val = np.array([103.939, 116.779, 123.68], dtype=np.float32)\n",
    "    mean_val = np.reshape(mean_val, (1, 3, 1, 1))\n",
    "    mean_val = np.tile(mean_val, (1, 1, 224, 224))\n",
    "    data_process = data - mean_val\n",
    "    data_process = data_process / 58.8\n",
    "    data_process = data_process.astype(np.float32)\n",
    "\n",
    "    proto_file_url = (\n",
    "        \"https://github.com/BVLC/caffe/raw/master/models\" + \"/bvlc_googlenet/deploy.prototxt\"\n",
    "    )\n",
    "    blob_file_url = \"http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel\"\n",
    "    proto_file = download_testdata(proto_file_url, \"inceptionv1.prototxt\", module=\"model\")\n",
    "    blob_file = download_testdata(blob_file_url, \"inceptionv1.caffemodel\", module=\"model\")\n",
    "    _test_network(data_process, proto_file, blob_file)\n",
    "\n",
    "\n",
    "@pytest.mark.skip(reason=\"See issue https://github.com/apache/tvm/issues/13227\")\n",
    "def test_forward_Inceptionv1():\n",
    "    \"\"\"Inceptionv4\"\"\"\n",
    "    data = np.random.randint(0, 256, size=(1, 3, 224, 224)).astype(np.float32)\n",
    "    _test_inceptionv1(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "xxx",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
